package com.oscar.ehome.presenter;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/10/20
 * 基类presenter
 */

public abstract class BasePresenter<T> {
    protected Reference<T> mViewRef;//view接口类型的弱引用

    public void attachView(T view) {
        if (view != null) {
            mViewRef = new WeakReference<T>(view);//建立关联
        }
    }

    protected T getView() {
        return mViewRef.get();
    }

    public boolean isViewAttached() {
        return mViewRef != null && mViewRef.get() != null;
    }

    public void detachView() {
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }

}
